package com.shengqi.xinnongren.work.ui.goods.activities;

import android.content.Context;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseActivity;
import com.shengqi.xinnongren.common.utils.ScreenUtils;
import com.shengqi.xinnongren.work.ui.goods.adapter.PicPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class BrowsePicActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.vp_pic)
    ViewPager mViewPager;
    @BindView(R.id.tv_indicator)
    TextView mTvIndicator;

    private static final String EXTRA_PIC_LIST = "extra_pic_list";
    private static final String EXTRA_PIC_POSITION = "extra_pic_position";

    List<String> mImageUrlList;
    int mPosition;

    public static void start(Context context, List<String> imageUrlList) {
        start(context, imageUrlList, 0);
    }

    public static void start(Context context, List<String> imageUrlList, int position) {
        ArrayList<String> strings = new ArrayList<>(imageUrlList);
        Intent intent = new Intent(context, BrowsePicActivity.class);
        intent.putStringArrayListExtra(EXTRA_PIC_LIST, strings);
        intent.putExtra(EXTRA_PIC_POSITION, position);
        context.startActivity(intent);
    }

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_browse_pic;
    }

    @Override
    protected void initView() {
        mTvIndicator.setPadding(0, 0, 0, ScreenUtils.getNavigationBarHeight(this));
    }

    @Override
    protected void initData() {
        mImageUrlList = getIntent().getStringArrayListExtra(EXTRA_PIC_LIST);
        mPosition = getIntent().getIntExtra(EXTRA_PIC_POSITION, 0);
        mTvIndicator.setText(String.format("%s/%s", mPosition + 1, mImageUrlList.size()));
        mViewPager.setAdapter(new PicPagerAdapter(mImageUrlList, this));
        mViewPager.setOnPageChangeListener(this);
        mViewPager.setCurrentItem(mPosition);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mTvIndicator.setText(String.format("%s/%s", position + 1, mImageUrlList.size()));
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
